package cn.iot.controller.sys;

import cn.iot.common.api.CommonResult;
import cn.iot.sys.entity.Grade;
import cn.iot.sys.entity.Profession;
import cn.iot.sys.entity.Student;
import cn.iot.sys.service.GradeService;
import cn.iot.sys.service.ProfessionService;
import cn.iot.sys.service.StudentService;
import cn.iot.sys.vo.AddForm;
import cn.iot.sys.vo.GradeDto;
import cn.iot.sys.vo.Page;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/grade/sys")
public class GradeController {
    @Autowired
    private GradeService gradeService;
    @Autowired
    private StudentService studentService;
    @Autowired
    private ProfessionService professionService;
    @PostMapping("/list")
    public CommonResult<Page<Grade> > list(@RequestBody GradeDto grade ){
        Page<Grade>  page=new Page<>();
        page.setCurrent ( grade.getPageNum () );
        grade.setPageNum ( (grade.getPageNum ()-1)*grade.getPageSize () );
        page.setSize ( grade.getPageSize () );
        page.setData ( gradeService.list ( grade ) );
        page.setTotal ( gradeService.getCount ( grade ) );

        return CommonResult.success (page,"查询成功");
    }
    @GetMapping("/profession")
    public CommonResult<Profession> getComm(){
        return CommonResult.success(professionService.list());
    }

    @PostMapping("/add/grade")
    public CommonResult<String> add(@RequestBody AddForm addon){
        gradeService.add ( addon );
        return CommonResult.success ("添加成功");
    }
    @PostMapping("/add/up")
    public CommonResult<String> up(@RequestBody AddForm addon){
        gradeService.update ( addon );
        return CommonResult.success ("添加成功");
    }
    @GetMapping("/del")
    public CommonResult<Integer> up(@RequestParam(value = "id") int  id){
        return CommonResult.success (gradeService.del ( id ));
    }
    @GetMapping("/stu/list")
    public CommonResult<Page<Student>> stuList( @RequestParam(value = "id") int  id,@RequestParam("size")int pageSize,@RequestParam("num")int pageNum){
        Page<Student> page=new Page<>();
        page.setCurrent ( pageNum );
        page.setSize ( pageSize );
        List<Student> list=studentService.getStudents ( id,(pageSize),(pageNum-1)*pageSize );
        page.setData ( list );
        page.setTotal ( list.size () );
        return CommonResult.success ( page);
    }
    @PostMapping("/qiDong")
    public CommonResult<String> qiDong(@RequestBody Grade[] addon){
        gradeService.qiDong ( addon );
        return CommonResult.success ();
    }
@PostMapping("/jinDong")
    public CommonResult<String> jinDong(@RequestBody Grade[] addon){
        gradeService.jinDong ( addon );
        return CommonResult.success ();
    }

}
